﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using HR60_PanelSoft.View.Base;

namespace HR60_PanelSoft.View.Base
{
	/// <summary>
	/// 表单基类，与BaseFormControl配合实现Form内的控件数据获取及自动提交
	/// 在BaseFormControl嵌套的情况下，只有顶层的Control有效
	/// </summary>
	public partial class BaseForm : BaseData
	{
		public BaseForm()
		{
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.AutoScroll = true;
			this.Name = "BaseForm";
			this.Size = new System.Drawing.Size(674, 372);
			this.ResumeLayout(false);

			// 支持滚轮
			this.MouseWheel += new System.Windows.Forms.MouseEventHandler(BaseForm_MouseWheel);
		}

		/// <summary>
		/// 获得主窗体
		/// </summary>
		protected WindowMain windowMain
		{
			get
			{
				return (WindowMain) this.ParentForm; 
			}
		}

		/// <summary>
		/// 已填充的数据
		/// </summary>
		protected IDictionary<string, object> bindDataMap;

		/// <summary>
		/// 数据是否已经填充，如果为false则不触发onFormChanged
		/// </summary>
		protected bool isBindDataFlag = false;

		/// <summary>
		/// 获得字段字典，作为提交数据集合
		/// </summary>
		/// <returns></returns>
		public IDictionary<string, object> getFields()
		{
			IDictionary<string, object> res = new Dictionary<string, object>();
			getFields(res, this);
			return res;
		}

		/// <summary>
		/// 获得字段字典
		/// </summary>
		/// <param name="res"></param>
		/// <param name="parent"></param>
		private static void getFields(IDictionary<string, object> res, Control parent)
		{
			foreach (Control control in parent.Controls)
			{
				if (typeof(BaseFormControl).IsAssignableFrom(control.GetType()))
				{
					BaseFormControl fc = (BaseFormControl)control;
					string id = fc.Id;
					object val = fc.Value;

					// 未设置id忽略
					if (id == null) continue;

					// 值无效忽略
					if (!fc.ValueEnabled) return;

					if (id.Length > 2 && id.Substring(id.Length - 2) == "[]")
					{
						// 列表字段
						id = id.Substring(0, id.Length - 2);
						if (!res.ContainsKey(id))
						{
							IList<object> list = new List<object>();
							list.Add(val);
							res.Add(id, list);
						}
						else
						{
							IList<object> list = (IList<object>)res[id];
							list.Add(val);
						}
					}
					else
					{
						// 普通字段
						res.Add(id, val);
					}
				}
				else
				{
					getFields(res, control);
				}
			}
		}

		/// <summary>
		/// 查找控件列表
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public IList<BaseFormControl> findChildFormControls()
		{
			return findChildFormControls(null);
		}

		/// <summary>
		/// 通过id查找控件列表
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public IList<BaseFormControl> findChildFormControls(string id)
		{
			IList<BaseFormControl> list = new List<BaseFormControl>();
			findChildFormControls(id, list, this);
			return list;
		}

		/// <summary>
		/// 通过id查找控件列表
		/// </summary>
		/// <param name="id"></param>
		/// <param name="res"></param>
		/// <param name="parent"></param>
		public static void findChildFormControls(string id, IList<BaseFormControl> res, Control parent)
		{
			foreach (Control control in parent.Controls)
			{
				if (typeof(BaseFormControl).IsAssignableFrom(control.GetType()))
				{
					BaseFormControl fc = (BaseFormControl)control;
					if (id == null || fc.Id == id)
					{
						res.Add(fc);
					}
				}
				else
				{
					findChildFormControls(id, res, control);
				}
			}
		}

		/// <summary>
		/// 刷新表单
		/// </summary>
		public void refreshForm(IDictionary<string, object> dataMap)
		{
			isBindDataFlag = false;
			bindDataMap = dataMap;
			refreshFormData(dataMap);
			isBindDataFlag = true;
		}

		/// <summary>
		/// 控件通知表单有数据改动
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void doFormChange(BaseFormControl sender, EventArgs e)
		{
			if (!isBindDataFlag) return;
			this.onFormChanged(sender, e);
		}

		/// <summary>
		/// 有数据改动时执行，由各Form重写
		/// </summary>
		public virtual void onFormChanged(BaseFormControl sender, EventArgs e)
		{
			// TODO:子类重写
		}

		/// <summary>
		/// 刷新表单时执行，由各Form重写
		/// </summary>
		/// <param name="dataMap"></param>
		public virtual void refreshFormData(IDictionary<string, object> dataMap)
		{
			// TODO:子类重写
		}

		// 滚轮控制滚动条
		void BaseForm_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			// 获取光标位置
			Point mousePoint = new Point(e.X, e.Y);

			// 换算成相对本窗体的位置
			mousePoint.Offset(this.Location.X, this.Location.Y);

			//判断是否在panel内
			if (this.RectangleToScreen(this.DisplayRectangle).Contains(mousePoint))
			{
				//滚动
				this.AutoScrollPosition = new Point(0, this.VerticalScroll.Value - e.Delta);
			}
		}

	}
}
